package com.miplus.ccrm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.admin.controller.vo.*;
import com.miplus.ccrm.admin.pojo.SysPrlink;
import com.miplus.ccrm.admin.pojo.SysRglink;
import com.miplus.ccrm.admin.pojo.SysRole;
import com.miplus.ccrm.admin.service.SysPrlinkService;
import com.miplus.ccrm.admin.service.SysRglinkService;
import com.miplus.ccrm.admin.service.SysRoleService;
import com.miplus.ccrm.admin.mapper.SysRoleMapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
* @author pc
* @description 针对表【sys_role(系统角色表)】的数据库操作Service实现
* @createDate 2024-03-04 09:30:12
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Resource
    private SysPrlinkService sysPrlinkService;

    @Resource
    private SysRglinkService sysRglinkService;

    @Override
    public List<NamesResVo> roleSelectList(String $usrid)
    {
        return getBaseMapper().roleSelectList($usrid);
    }

    @Override
    public IPage<RoleInfoResVo> roleList(RoleInfoReqVo paras)
    {
        return baseMapper.roleList(new Page<>(paras.getPageNum(), paras.getPageSize()),paras);
    }

    @Override
    public CmdResVo updateStatus(RoleReqVo paras)
    {
        SysRole role=new SysRole();
        role.setRoleid(paras.getRoleid());
        role.setStatus(paras.getStatus());
        int ret=baseMapper.updateById(role);
        return new CmdResVo(ret,role.getRoleid());
    }

    @Override
    public CmdResVo updateRole(RoleEditReqVo paras)
    {
        SysRole role=new SysRole();
        role.setRoleid(paras.getRoleid());
        role.setDsc(paras.getDsc());
        role.setType(paras.getTypeid());
        role.setStatus(paras.getStatus());
        int ret=baseMapper.updateById(role);
        if(ret>0) {
            if (paras.getRemovePermission() != null || paras.getAddPermission() != null) {
                sysPrlinkService.remove(
                        new QueryWrapper<SysPrlink>().eq("roleid", paras.getRoleid())
                );
                if(JPubUtils.IsNotEmpty(paras.getPermission())){
                    List<SysPrlink> prlist = new ArrayList<>();
                    for (String prem : paras.getPermission()) {
                        SysPrlink pr = new SysPrlink();
                        pr.setRoleid(paras.getRoleid());
                        pr.setPermissionid(prem);
                        prlist.add(pr);
                    }
                    sysPrlinkService.saveBatch(prlist);
                }
            }
            sysRglinkService.remove(
                    new QueryWrapper<SysRglink>().eq("roleid",paras.getRoleid())
            );
            if(JPubUtils.IsNotEmpty(paras.getGrouplist())){
                List<SysRglink> rglist = new ArrayList<>();
                for (String prem : paras.getGrouplist()) {
                    SysRglink pr = new SysRglink();
                    pr.setRoleid(paras.getRoleid());
                    pr.setGrpid(prem);
                    rglist.add(pr);
                }
                sysRglinkService.saveBatch(rglist);
            }
        }

        return new CmdResVo(ret,role.getRoleid());
    }

    @Override
    public CmdResVo insertRole(RoleEditReqVo paras)
    {
        SysRole role=new SysRole();
        String roleid=paras.getRoleid();
        if(JPubUtils.IsEmpty(roleid)){
            SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
            roleid=sdf.format(new Date());
        }
        role.setRoleid(roleid);
        role.setDsc(paras.getDsc());
        role.setType(paras.getTypeid());
        role.setStatus(paras.getStatus());
        int ret=baseMapper.insert(role);
        if(ret>0){
            if(JPubUtils.IsNotEmpty(paras.getPermission())){
                List<SysPrlink> prlist=new ArrayList<>();
                for(String prem : paras.getPermission()){
                    SysPrlink pr=new SysPrlink();
                    pr.setRoleid(roleid);
                    pr.setPermissionid(prem);
                    prlist.add(pr);
                }
                if(prlist.size()>0) {
                    sysPrlinkService.saveBatch(prlist);
                }
            }

            if(JPubUtils.IsNotEmpty(paras.getGrouplist())){
                List<SysRglink> rglist = new ArrayList<>();
                for (String prem : paras.getGrouplist()) {
                    SysRglink pr = new SysRglink();
                    pr.setRoleid(roleid);
                    pr.setGrpid(prem);
                    rglist.add(pr);
                }
                sysRglinkService.saveBatch(rglist);
            }
        }

        return new CmdResVo(ret,roleid);
    }

    @Override
    public CmdResVo deleteRole(String roleid)
    {
        int ret=baseMapper.deleteById(roleid);
        if(ret>0){
            sysPrlinkService.remove(
                    new QueryWrapper<SysPrlink>().eq("roleid",roleid)
            );
            sysRglinkService.remove(
                    new QueryWrapper<SysRglink>().eq("roleid",roleid)
            );
        }
        return new CmdResVo(ret,roleid);
    }

    @Override
    public List<PermissionIdResVo> rolePermission(String roleid)
    {
        List<SysPrlink> rlist=sysPrlinkService.getBaseMapper().selectList(
                new QueryWrapper<SysPrlink>().eq("roleid",roleid)
        );
        List<PermissionIdResVo> prlist=new ArrayList<>();
        if(rlist!=null){
            for(SysPrlink prem : rlist){
                PermissionIdResVo pr=new PermissionIdResVo();
                pr.setPermission(prem.getPermissionid());
                prlist.add(pr);
            }
        }
        return prlist;
    }

    @Override
    public List<String> roleGroup(String roleid)
    {
        List<SysRglink> rlist=sysRglinkService.list(
                new QueryWrapper<SysRglink>().eq("roleid",roleid)
        );
        List<String> prlist=new LinkedList<>();
        if(rlist!=null){
            for(SysRglink prem : rlist){
                prlist.add(prem.getGrpid());
            }
        }
        return prlist;
    }
}




